package logic

import (
	"context"
	"user/common"

	"user/internal/svc"
	"user/pb/user"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetUserByMobileLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetUserByMobileLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUserByMobileLogic {
	return &GetUserByMobileLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// GetUserByMobile 根据mobile获取用户
func (l *GetUserByMobileLogic) GetUserByMobile(in *user.GetUserByMobileRequest) (*user.UserInfo, error) {
	u, err := l.svcCtx.UserExtendModel.FindOneByMobile(l.ctx, in.Mobile, common.UserStatusEnabled)
	if err != nil {
		return nil, err
	}

	userInfo := &user.UserInfo{
		Id:       u.Id,
		Name:     u.Name,
		Mobile:   u.Mobile,
		Email:    u.Email,
		Avatar:   u.Avatar,
		Status:   u.Status,
		CreateAt: u.CreatedAt.Unix(),
		UpdateAt: u.UpdatedAt.Unix(),
	}
	return userInfo, nil
}
